# -*- coding: utf-8 -*-
"""
@file: check_license.py
@desc:
@author: Jaden Wu
@time: 2021/1/15 10:46
"""
import rsa
import json
import base64
from license.get_feature_code import get_feature_code


priv_key = rsa.PrivateKey(20816281982197868326789835083014692149633441268803139592665530523715806090719177894080910285992406990989999107118274521358837362975908714419071078579215670648684310478140959568057981425618545019799571885606891762626798931273276930756678789373105419411089955291481982000295370567108758412729101722833721011490322957505658923210267085554949117522306832213612526051457600207291796561570176237727571158132904149190987472909122246622548761004658309316711188258124249425480648203620977467828945027494797248225783618421814597208097650824834354868536745452934816285947439848193905046917062479167569622292880276163476719974671, 65537, 15228915817301051235123143154099553379103788319179018430961770091552528172263327636443586440822618453545424679034633721425619363187862111807632054161146749850036131501059652523766848916378637403593546754158842065564558975217488376368459362425544538796162762964514626989123118806332247903576750708805492891296537612246094120684084513373765515170910906575261261499028235436904848119398552718075912054818469716430251488718832462107273298806755763905606362912369229594521155883103763719940317228252058048169181013896833217292244196755481443184699534147929640826463637113594704429968175248441756702876585873089962629115729, 147733475542691462051239612132356076032938520667154936090264690269270818756414186071451905237012617024166032387554672419191266231418319588555162682122714719952008054647641282122927566553077631100549761396767518586952715714735793491656934873360004427055460708779085261703144403840900842278240991145364866167747, 140904300164402872546410474238140335941684870042578497384324091075981063361400030120619975862603045000230029013493426847491099580766112885839164812059971281340324270464777329623579505993337514283211246009388659531297858758286369703003921674288346442716544577331306160994498285205961963253641321377831521258693)


def get_lic_info(lic_path='license.cer'):
    try:
        with open(lic_path, 'r') as fr:
            license_content = fr.read()
        crypto = base64.b64decode(license_content)
        message = rsa.decrypt(crypto, priv_key)
        info = json.loads(message.decode('utf8'))
        return info
    except Exception as e:
        _ = e
        return {}


def check_lic(lic_info: dict):
    feature_code = get_feature_code()
    is_right_machine = (feature_code == lic_info.get('feature_code'))
    return is_right_machine


if __name__ == '__main__':
    print(check_lic(get_lic_info('.\\certificate\\license_20210128122630.cer')))
